package com.talkweb.securitycamera.data.hadoopFile;

import com.talkweb.securitycamera.common.constans.FileType;
import com.talkweb.securitycamera.common.interfaces.dataprocess.IFileProcessor;
import com.talkweb.securitycamera.data.bean.MediaEntity;
import ohos.app.Context;

import java.util.List;

/**
 * @author : frank
 * @date : 2021/2/7 9:40
 */
public abstract class BaseMemoryPolicy implements IMemoryPolicy {

    abstract BaseMediaProcessor getMediaProcessor(Context context);

    abstract int getDeleteCount();

    @Override
    public boolean isMemInfoOut() {
        return false;
    }

    @Override
    public void cleanMemory(Context context, FileType fileType) {
        BaseMediaProcessor mediaProcessor = getMediaProcessor(context);

        List<MediaEntity> mediaList = mediaProcessor.getFileList(fileType);
        mediaList.sort((t1, t2) -> (int) (t1.getCreateTime() - t2.getCreateTime()));

        int deleteCount = getDeleteCount();
        int size = mediaList.size();
        deleteCount = mediaList.size() <= deleteCount ? size : deleteCount;

        for (int i = 0; i < deleteCount; i++) {
            mediaList.get(i).delete();
            //TODO 更新数据库
        }
    }
}
